package com.keller.guli.service.edu.controller.admin;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keller.guli.common.base.result.R;
import com.keller.guli.service.edu.entity.Teacher;
import com.keller.guli.service.edu.entity.vo.TeacherQueryVo;
import com.keller.guli.service.edu.feign.OssFileService;
import com.keller.guli.service.edu.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author keller
 * @since 2020-07-21
 */
//@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;

    //查询老师列表
    @GetMapping("/list")
    public R listAll(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    //删除
    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R deleteTeacher(@PathVariable String id){
        //删除图片
        teacherService.removeAvatarById(id);
        boolean result = teacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }

    }

    //分页查询
    @GetMapping("/list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable long limit,
                      @ApiParam("讲师列表查询对象") TeacherQueryVo teacherQueryVo){
        Page<Teacher> page1 = new Page<>(page, limit);
        IPage<Teacher> teacherPage = teacherService.selectPage(page1, teacherQueryVo);
        long total = teacherPage.getTotal();
        List<Teacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    //新增讲师
    @PostMapping("/save")
    public R save(@RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    //根据id查询讲师
    @GetMapping("/get/{id}")
    public R getTeacherById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher).message("获取讲师成功");
    }


    //根据id更新讲师
    @ApiOperation("根据id获取讲师信息")
    @PutMapping("update")
    public R updateById(@ApiParam("讲师对象") @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }

    //批量删除
    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "讲师id列表", required = true)
                        @RequestBody List<String> idList){
        System.out.println(idList.toString());
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }

    @GetMapping("/message1")
    public String message1() {
        return "message1";
    }

    @GetMapping("/message2")
    public String message2() {
        return "message2";
    }
}


